package com.ikdong.toodle.widget;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.TimeZone;

import com.ikdong.toodle.model.Account;
import com.ikdong.toodle.model.Folder;
import com.ikdong.toodle.model.Goal;
import com.ikdong.toodle.model.Task;
import com.ikdong.toodle.model.TaskContext;
import com.ikdong.toodle.service.SyncService;
import com.ikdong.toodle.util.AccountManager;
import com.ikdong.toodle.util.ComUtil;
import com.ikdong.toodle.util.Constant;
import com.ikdong.toodle.util.ContextManager;
import com.ikdong.toodle.util.FolderManager;
import com.ikdong.toodle.util.GoalManager;
import com.ikdong.toodle.util.TaskManager;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.support.v4.app.Fragment;

public abstract class BasicTabFrame extends Fragment {
	protected IntentFilter refFilter = new IntentFilter(Constant.NOTIFICATION_TASK_CHANGE);
	protected IntentFilter syncFilter = new IntentFilter(Constant.SERVICE_SYNC);
	protected SyncReceiver syncReceiver = new SyncReceiver();
	
	@Override
	public void onResume(){
		super.onResume();
		this.getActivity().registerReceiver(syncReceiver, syncFilter);
	}
    
    @Override
	public void onPause(){
		super.onPause();
		this.getActivity().unregisterReceiver(syncReceiver);
	}
    
    abstract void refresh();
	
	protected class SyncReceiver extends BroadcastReceiver {
		@Override
		public void onReceive(Context context, Intent intent) {
			if(!intent.getExtras().getBoolean(SyncService.ERROR_MESSAGE)){
				refresh();
			}
		}
	}
	
	protected List<Task> filterTaskByMain(List<Task> tList,String tab){
		List<Task> ts = new ArrayList<Task>();
		List<Task> tasks = new ArrayList<Task>();
		for(int i=0; i<tList.size(); i++){
			Task t = tList.get(i);
			if(t.getCompleted()==null || t.getCompleted()<=0){
				tasks.add(t);
			}
		}
		
		AccountManager am = new AccountManager(this.getActivity());
		Account account = am.getAccount();		
		if(account==null)
			return ts;
		
		if("All".equalsIgnoreCase(tab)){
			ts = tasks;
		}else if("Hotlist".equalsIgnoreCase(tab)){
			Calendar calendar = Calendar.getInstance();
			calendar.setTimeZone(TimeZone.getTimeZone("GMT+0"));
			calendar.set(Calendar.HOUR_OF_DAY, 0);
			calendar.set(Calendar.MINUTE, 0);
			for(Task t : tasks){
				boolean status = account.getHotliststatus()==0 ? true:t.getStatus().equals(Long.valueOf(1));
				if(!status) continue;
				boolean priority = account.getHotlistpriority()==null ? true:t.getPriority()>=account.getHotlistpriority().intValue();
				if(!priority) continue;
				boolean star = account.getHotliststar()==0 ? true:t.getStar().equals(Long.valueOf(1));
				if(!star) continue;
				boolean duedate = true;
				if(account.getHotlistduedate()==null){
					duedate = true;
				}else{
					calendar.add(Calendar.DAY_OF_MONTH, account.getHotlistduedate().intValue());					
					long end = ComUtil.parseUnixStamp(calendar.getTime());
					duedate = t.getDuedate()<=end;  
				}
				
				if(status && priority && star && duedate){
					ts.add(t);
				}
			}	
		}else if("Starred".equalsIgnoreCase(tab)){
			for(Task t : tasks){
				if(Long.valueOf(1).equals(t.getStar())){
					ts.add(t);
				}
			}			
		}else if("Completed".equalsIgnoreCase(tab)){
			ts = new TaskManager(this.getActivity()).getCompleted();
		}
		return ts;
	}
	
	protected List<Task> filterTaskByStatus(List<Task> tList,String tab){
		List<Task> ts = new ArrayList<Task>();
		List<Task> tasks = new ArrayList<Task>();
		for(int i=0; i<tList.size(); i++){
			Task t = tList.get(i);
			if(t.getCompleted()==null || t.getCompleted()<=0){
				tasks.add(t);
			}
		}
		if("All".equalsIgnoreCase(tab)){
			ts = tasks;
		}else if("Next Action".equalsIgnoreCase(tab)){
			for(Task t : tasks){
				if(Long.valueOf(1).equals(t.getStatus())){
					ts.add(t);
				}
			}
		}else if("Active".equalsIgnoreCase(tab)){
			for(Task t : tasks){
				if(Long.valueOf(2).equals(t.getStatus())){
					ts.add(t);
				}
			}
		}else if("Planning".equalsIgnoreCase(tab)){
			for(Task t : tasks){
				if(Long.valueOf(3).equals(t.getStatus())){
					ts.add(t);
				}
			}
		}else if("Delegated".equalsIgnoreCase(tab)){
			for(Task t : tasks){
				if(Long.valueOf(4).equals(t.getStatus())){
					ts.add(t);
				}
			}
		}else if("Waiting".equalsIgnoreCase(tab)){
			for(Task t : tasks){
				if(Long.valueOf(5).equals(t.getStatus())){
					ts.add(t);
				}
			}
		}else if("Hold".equalsIgnoreCase(tab)){
			for(Task t : tasks){
				if(Long.valueOf(6).equals(t.getStatus())){
					ts.add(t);
				}
			}
		}else if("Postponed".equalsIgnoreCase(tab)){
			for(Task t : tasks){
				if(Long.valueOf(7).equals(t.getStatus())){
					ts.add(t);
				}
			}
		}else if("Someday".equalsIgnoreCase(tab)){
			for(Task t : tasks){
				if(Long.valueOf(8).equals(t.getStatus())){
					ts.add(t);
				}
			}
		}else if("Canceled".equalsIgnoreCase(tab)){
			for(Task t : tasks){
				if(Long.valueOf(9).equals(t.getStatus())){
					ts.add(t);
				}
			}
		}else if("Reference".equalsIgnoreCase(tab)){
			for(Task t : tasks){
				if(Long.valueOf(10).equals(t.getStatus())){
					ts.add(t);
				}
			}
		}else if("No Status".equalsIgnoreCase(tab)){
			for(Task t : tasks){
				if(t.getStatus()==0){
					ts.add(t);
				}
			}
		}
		return ts;
	}
	
	protected List<Task> filterTaskByPriority(List<Task> tList,String tab){
		List<Task> ts = new ArrayList<Task>();
		List<Task> tasks = new ArrayList<Task>();
		for(int i=0; i<tList.size(); i++){
			Task t = tList.get(i);
			if(t.getCompleted()==null || t.getCompleted()<=0){
				tasks.add(t);
			}
		}
		if("All".equalsIgnoreCase(tab)){
			ts = tasks;
		}else if("Top".equalsIgnoreCase(tab)){
			for(Task t : tasks){
				if(Long.valueOf(3).equals(t.getPriority())){
					ts.add(t);
				}
			}
		}else if("High".equalsIgnoreCase(tab)){
			for(Task t : tasks){
				if(Long.valueOf(2).equals(t.getPriority())){
					ts.add(t);
				}
			}
		}else if("Medium".equalsIgnoreCase(tab)){
			for(Task t : tasks){
				if(Long.valueOf(1).equals(t.getPriority())){
					ts.add(t);
				}
			}
		}else if("Low".equalsIgnoreCase(tab)){
			for(Task t : tasks){
				if(Long.valueOf(0).equals(t.getPriority())){
					ts.add(t);
				}
			}
		}else if("Negative".equalsIgnoreCase(tab)){
			for(Task t : tasks){
				if(Long.valueOf(-1).equals(t.getPriority())){
					ts.add(t);
				}
			}
		}
		return ts;
	}
	
	protected List<Task> filterTaskByDuedate(List<Task> tList,String tab){
		List<Task> ts = new ArrayList<Task>();
		List<Task> tasks = new ArrayList<Task>();
		for(int i=0; i<tList.size(); i++){
			Task t = tList.get(i);
			if(t.getCompleted()==null || t.getCompleted()<=0){
				tasks.add(t);
			}
		}
		Calendar calendar = Calendar.getInstance();
		calendar.setTimeZone(TimeZone.getTimeZone("GMT+0"));
		if("All".equalsIgnoreCase(tab)){
			ts = tasks;
		}else if("Started".equalsIgnoreCase(tab)){
			long now = ComUtil.parseUnixStamp(calendar.getTime());
			for(Task t : tasks){
				if(t.getStartdate()>0 && now>t.getStartdate()){
					ts.add(t);
				}
			}
		}else if("Overdue".equalsIgnoreCase(tab)){
			long now = ComUtil.parseUnixStamp(calendar.getTime());
			for(Task t : tasks){
				if(t.getDuedate()>0 && now>t.getDuedate()){
					ts.add(t);
				}
			}
		}else if("Today".equalsIgnoreCase(tab)){
			String today = ComUtil.formateDate(calendar.getTime(), "yyyy-MM-dd");			
			for(Task t : tasks){
				String duedate = ComUtil.formateDate(ComUtil.parseDateByUS(t.getDuedate()), "yyyy-MM-dd");
				if(today.equals(duedate)){
					ts.add(t);
				}
			}
		}else if("Tomorrow".equalsIgnoreCase(tab)){
			calendar.add(Calendar.DAY_OF_MONTH, 1);
			String tomorrow = ComUtil.formateDate(calendar.getTime(), "yyyy-MM-dd");			
			for(Task t : tasks){
				String duedate = ComUtil.formateDate(ComUtil.parseDateByUS(t.getDuedate()), "yyyy-MM-dd");
				if(tomorrow.equals(duedate)){
					ts.add(t);
				}
			}
		}else if("Next 7 Days".equalsIgnoreCase(tab)){
			calendar.add(Calendar.DAY_OF_MONTH, 7);
			for(Task t : tasks){
				if(t.getDuedate()>0 && calendar.getTimeInMillis()>ComUtil.parseDateByUS(t.getDuedate()).getTime()){
					ts.add(t);
				}
			}
			
		}else if("7 to 14 Days".equalsIgnoreCase(tab)){
			calendar.add(Calendar.DAY_OF_MONTH, 14);
			for(Task t : tasks){
				if(t.getDuedate()>0 && calendar.getTimeInMillis()>ComUtil.parseDateByUS(t.getDuedate()).getTime()){
					ts.add(t);
				}
			}
		}else if("Next 30 Days".equalsIgnoreCase(tab)){
			calendar.add(Calendar.DAY_OF_MONTH, 30);
			for(Task t : tasks){
				if(t.getDuedate()>0 && calendar.getTimeInMillis()<ComUtil.parseDateByUS(t.getDuedate()).getTime()){
					ts.add(t);
				}
			}
		}else if("No Due-Date".equalsIgnoreCase(tab)){
			for(Task t : tasks){
				if(t.getDuedate()==null || t.getDuetime()==0){
					ts.add(t);
				}
			}
		}
		return ts;
	}
	
	protected List<Task> filterTaskByFolder(List<Task> tList,String tab){
		List<Task> ts = new ArrayList<Task>();
		List<Task> tasks = new ArrayList<Task>();
		for(int i=0; i<tList.size(); i++){
			Task t = tList.get(i);
			if(t.getCompleted()==null || t.getCompleted()<=0){
				tasks.add(t);
			}
		}
		
		if("All".equalsIgnoreCase(tab)){
			ts = tasks;
		}else if("No Folder".equalsIgnoreCase(tab)){
			for(Task t : tasks){
				if(t.getFolder()==null || t.getFolder()==0){
					ts.add(t);
				}
			}
		}else{
			Folder folder = new FolderManager(this.getActivity()).get(tab);
			if(folder ==  null)
				return ts;
			
			for(Task t : tasks){
				if(folder.getId().equals(t.getFolder())){
					ts.add(t);
				}
			}
		}
		return ts;
	}
	
	protected List<Task> filterTaskByContext(List<Task> tList,String tab){
		List<Task> ts = new ArrayList<Task>();
		List<Task> tasks = new ArrayList<Task>();
		for(int i=0; i<tList.size(); i++){
			Task t = tList.get(i);
			if(t.getCompleted()==null || t.getCompleted()<=0){ 
				tasks.add(t);
			}
		}
		
		if("All".equalsIgnoreCase(tab)){
			ts = tasks;
		}else if("No Context".equalsIgnoreCase(tab)){
			for(Task t : tasks){
				if(t.getContext()==null || t.getContext()==0){
					ts.add(t);
				}
			}
		}else{
			TaskContext ctx = new ContextManager(this.getActivity()).getContext(tab);
			if(ctx ==  null)
				return ts;
			
			for(Task t : tasks){
				if(ctx.getId().equals(t.getContext())){
					ts.add(t);
				}
			}
		}
		return ts;
	}
	
	protected List<Task> filterTaskByGoal(List<Task> tList,String tab){
		List<Task> ts = new ArrayList<Task>();
		List<Task> tasks = new ArrayList<Task>();
		for(int i=0; i<tList.size(); i++){
			Task t = tList.get(i);
			if(t.getCompleted()==null || t.getCompleted()<=0){ 
				tasks.add(t);
			}
		}
		
		if("All".equalsIgnoreCase(tab)){
			ts = tasks;
		}else if("No Goal".equalsIgnoreCase(tab)){
			for(Task t : tasks){
				if(t.getGoal()==null || t.getGoal()==0){
					ts.add(t);
				}
			}
		}else{
			Goal goal = new GoalManager(this.getActivity()).getGoal(tab);
			if(goal ==  null)
				return ts;
			
			for(Task t : tasks){
				if(goal.getId().equals(t.getGoal())){
					ts.add(t);
				}
			}
		}
		return ts;
	}
}
